package com.fifth.apartment.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fifth.apartment.entity.FacilityInfo;
import com.fifth.apartment.result.Results;
import com.fifth.apartment.service.FacilityInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "配套管理")
@Api(tags = "配置接口")
@RestController
@RequestMapping("/admin/facility")
public class FacilityController {
    @Autowired
    private FacilityInfoService facilityInfoService;

    @Operation(summary = "[根据类型]查询配套信息列表")
    @ApiOperation("[根据类型]查询配套信息列表")
    @GetMapping("list")
    public Results<List<FacilityInfo>> listFacility(@RequestParam(required = false) Long type) {
        LambdaQueryWrapper<FacilityInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type != null,FacilityInfo::getType,type);
        List<FacilityInfo> list = facilityInfoService.list(queryWrapper);
        return Results.ok(list);
    }

//    @Operation(summary = "新增或修改配套信息")
//    @ApiOperation("新增或修改配套信息")
//    @PostMapping("saveOrUpdate")
//    public Result saveOrUpdate(@RequestBody FacilityInfo facilityInfo) {
//        try {
//            boolean save = facilityInfoService.saveOrUpdate(facilityInfo);
//            if (save){
//                return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return new Result(false , MessageConstant.ADD_CHECKITEM_FAIL);
//    }
//
//    @Operation(summary = "根据id删除配套信息")
//    @DeleteMapping("deleteById")
//    @ApiOperation("根据id删除配套信息")
//    public Result delete(@PathVariable("id") Long id){
//        try {
//            boolean flag = facilityInfoService.removeById(id);
//            if(flag){
//                return new Result(true , MessageConstant.DELETE_CHECKITEM_SUCCESS);
//            }
//        }catch (RuntimeException e){
//            return new Result(false , e.getMessage());
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return new Result(false , MessageConstant.DELETE_CHECKITEM_FAIL);
//    }
@Operation(summary = "新增或修改配套信息")
@ApiOperation("新增或修改配套信息")
@PostMapping("saveOrUpdate")
public Results saveOrUpdate(@RequestBody FacilityInfo facilityInfo) {
    facilityInfoService.saveOrUpdate(facilityInfo);
    return Results.ok();
}

    @Operation(summary = "根据id删除配套信息")
    @DeleteMapping("deleteById")
    @ApiOperation("根据id删除配套信息")
    public Results removeFacilityById(@RequestParam Long id) {
        facilityInfoService.removeById(id);
        return Results.ok();
    }

}
